package com.opgea.crm.services;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.opgea.crm.common.SetupsAttributeQualifier;
import com.opgea.crm.common.SetupsModuleQualifier;
import com.opgea.crm.common.vo.CommonAppvo;
import com.opgea.crm.domain.entities.SetupsValue;
import com.opgea.crm.repository.SetupsRepo;

@Service
public class SetupServiceImpl implements SetupService {

	@Autowired
        private SetupsRepo setupsRepo;


	public List<CommonAppvo> listSetupAttributesByModuleid(Integer moduleid){
		List<CommonAppvo> atList=new ArrayList<CommonAppvo>();
		SetupsModuleQualifier module=SetupsModuleQualifier.values()[moduleid];
		SetupsAttributeQualifier arr[]=module.getAttributes();
		if(arr!=null){
		for(SetupsAttributeQualifier data:arr)
		{
			CommonAppvo vo=new CommonAppvo();
			vo.setData(data.name());
			vo.setIndex(data.ordinal());
			atList.add(vo);
		}
		}
		return atList;
	}


	public List<CommonAppvo> listSetupModules() {
		List<CommonAppvo> list=new ArrayList<CommonAppvo>();
		for(SetupsModuleQualifier data:SetupsModuleQualifier.values())
		{
			CommonAppvo vo=new CommonAppvo();
			vo.setData(data.name());
			vo.setIndex(data.ordinal());
			list.add(vo);
		}
		return list;
	}


	
	
	public List<SetupsValue> listSetupValue(int setupModuleid,
			int setupAttributeid) {
		 
		  
		return setupsRepo.listSetupValue(SetupsModuleQualifier.values()[setupModuleid],SetupsAttributeQualifier.values()[setupAttributeid]);
	}
	
	

	@Transactional(propagation=Propagation.REQUIRED)
	public void removeAttrValue(long attrValueid) {
		setupsRepo.removeAttrValue(attrValueid);
		
	}

	@Transactional(propagation=Propagation.REQUIRED)
	public void saveAttrValue(int setupModuleid,int setupAttributeid,String setupAttrValue) {
		SetupsValue setupsValue=new SetupsValue();
		setupsValue.setFieldValue(setupAttrValue);
		setupsValue.setSetupsAttributeQualifier(SetupsAttributeQualifier.values()[setupAttributeid]);
		setupsValue.setSetupsModuleQualifier(SetupsModuleQualifier.values()[setupModuleid]);
		setupsRepo.saveAttrValue(setupsValue);
		
	}

	public SetupsValue findById(long attrValueid)
	
	{
		return setupsRepo.findById(attrValueid);
	}



	
}
